package controller.servlet.user;

import java.io.IOException;
import java.util.HashMap;
import java.util.Set;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.User;
import controller.dao.Dao_User;

public class editUser extends HttpServlet {
	private static final long serialVersionUID = 1L;
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int codigo = Integer.parseInt(request.getParameter("cod"));
		Dao_User daou = new Dao_User();
		User u = daou.list(codigo);
		String [] profiles = u.getProfileType().split(",");
		HashMap<String,String> profs = new HashMap<String,String>();
		profs.put("Category", "/admin/category.ja");
		profs.put("Payment Forms", "/admin/payment-forms.ja");
		profs.put("Sales Report", "/admin/sales-report.ja");
		profs.put("Client", "/admin/client.ja");
		profs.put("Product", "/admin/product.ja");
		profs.put("Admin", "Admin");
		profs.put("User", "/admin/user.ja");
		Set<String> keys = profs.keySet();
		request.setAttribute("keys", keys);
		request.setAttribute("user", u);
		request.setAttribute("profs", profs);
		request.setAttribute("profiles", profiles);

		RequestDispatcher rd = request.getRequestDispatcher("/admin/register-user.jsp");
		rd.forward(request, response);
	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int cod = Integer.parseInt(request.getParameter("cod"));
		
		Dao_User daoe = new Dao_User();
		User user = daoe.list(cod);
		
		String name = request.getParameter("name");
		String login = request.getParameter("login");
		String email = request.getParameter("email");
		
		String profiles = "";
		String [] profile = request.getParameterValues("profile");
		
		for(String p: profile)
		{
			profiles+=p+","; 
		}
	
		user.setName(name);
		user.setLogin(login);
		user.setEmail(email);
		user.setProfileType(profiles);
		
		daoe.update(user);
		
		response.sendRedirect("/ShoppingCart.Com_V6/admin/user.ja");
	}

}
